import { CHANGE_INPUT_VALUE, ADD_TASKS, REMOVE_TASKS, GET_TASK_LIST } from "./actionTypes";

const defaultState = {
  inputValue: "",
  // list: ["数据1", "数据2", "数据3"],
  list: [] // axios
};

export default (state = defaultState, action) => {
  /*
    Reducer里只能接收state，不能改变state
    state值只能传递，不能使用
  */
  if (action.type === CHANGE_INPUT_VALUE) {
    let newState = JSON.parse(JSON.stringify(state));
    newState.inputValue = action.value;
    return newState;
  }
  if (action.type === ADD_TASKS) {
    let newState = JSON.parse(JSON.stringify(state));
    newState.list.push(newState.inputValue);
    newState.inputValue = "";
    return newState;
  }
  if (action.type === REMOVE_TASKS) {
    let newState = JSON.parse(JSON.stringify(state));
    newState.list.splice(action.index, 1);
    return newState;
  }
  if(action.type === GET_TASK_LIST) {
    let newState = JSON.parse(JSON.stringify(state))
    newState.list = action.list
    return newState
  }

  return state;
};
